package com.jshoperxms.controller.mall.backstage.sysrole;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.mall.backstage.vo.sysrole.SysModuleRoleVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.SysRoleEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.SysModuleActionT;
import com.jshoperxms.entity.SysRoleModuleActionRpT;
import com.jshoperxms.service.SysModuleActionTService;
import com.jshoperxms.service.SysRoleModuleActionRpTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/10/26.
 * Des:
 */
@Controller
@RequestMapping("/mall/sysroleaction")
public class SysRoleModuleActionRpTController extends BaseTController implements IBaseCtrl<SysRoleModuleActionRpT> {


    @Resource
    private SysRoleModuleActionRpTService sysRoleModuleActionRpTService;

    @Resource
    private SysModuleActionTService sysModuleActionTService;

    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();



    @Override
    public ResponseEntity save(@RequestBody SysRoleModuleActionRpT rq) {
        return null;
    }


    //获取角色已有的权限信息
    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody SysRoleModuleActionRpT rq) {
        if(StringUtils.isBlank(rq.getSysroleid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            Criterion criterion=Restrictions.and(Restrictions.eq("sysroleid",rq.getSysroleid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            List<SysRoleModuleActionRpT>beanlist=this.sysRoleModuleActionRpTService.findByCriteria(SysRoleModuleActionRpT.class,criterion);
            if(!beanlist.isEmpty()){
                responsedata.put(TagWords.BEANLIST,beanlist);
                responsedata.put(TagWords.SUCCESS, true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);
    }


    //删除单个角色权限
    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody SysRoleModuleActionRpT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            String ids[]=StringUtils.split(rq.getId(), TagWords.SPLITDOT);
            for(String s:ids){
                SysRoleModuleActionRpT bean=this.sysRoleModuleActionRpTService.findByPK(SysRoleModuleActionRpT.class, s);
                if(bean!=null){
                    this.sysRoleModuleActionRpTService.delete(bean);
                }
            }
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity update(@RequestBody SysRoleModuleActionRpT rq) {
        return null;
    }


    //更新角色权限关系
    @RequestMapping(value="/updateMulitRoleModuleAction",method= RequestMethod.POST)
    public ResponseEntity  updateMulitSysRoleModuleAction(@RequestBody SysRoleModuleActionRpT rq){
        if(StringUtils.isBlank(rq.getSysmoduleactionid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            this.sysRoleModuleActionRpTService.saveOrUpdateSysRoleModuleActionTransactional(rq);
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }


    //查询出所有权限
    //TODO 前端角色权限展示
    @RequestMapping(value="/findAll",method= RequestMethod.POST)
    @Override
    public ResponseEntity findAll() {
        Criterion criterion= Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState());
        List<SysModuleActionT> list=this.sysModuleActionTService.findByCriteria(SysModuleActionT.class,criterion, Order.desc(TagWords.UPDATETIME));
        if(!list.isEmpty()){
//            List<SysModuleRoleVo>roles=new ArrayList<>();
//            SysModuleRoleVo sysModuleRoleVo= new SysModuleRoleVo();
//            for(SysModuleActionT s:list){
//                sysModuleRoleVo.setModulename(s.getSysmodulename());
//                if(!StringUtils.equals(s.getSysmodulename(),sysModuleRoleVo.getModulename())){
//                    SysModuleRoleVo sysModuleRoleVo1= new SysModuleRoleVo();
//                    sysModuleRoleVo1.setModulename(s.getSysmodulename());
//                    for(SysModuleActionT s2:list){
//                        if(StringUtils.equals(s2.getSysmodulename(),sysModuleRoleVo1.getModulename())){
//                            List<SysModuleActionT>currentRoles=new ArrayList<>();
//                            currentRoles.add(s2);
//                            sysModuleRoleVo1.setList(currentRoles);
//                        }
//                    }
//                    roles.add(sysModuleRoleVo1);
//                }else{
//                    for(SysModuleActionT s2:list){
//                        if(StringUtils.equals(s2.getSysmodulename(),sysModuleRoleVo.getModulename())){
//                            List<SysModuleActionT>currentRoles=new ArrayList<>();
//                            currentRoles.add(s2);
//                            sysModuleRoleVo.setList(currentRoles);
//                        }
//                    }
//                    roles.add(sysModuleRoleVo);
//                }


//            }
            responsedata.put(TagWords.BEANLIST,list);
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
       return  null;
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {

    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {

    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {

    }

    @Override
    public void processPageList(List<SysRoleModuleActionRpT> list) {

    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
